import {
    ADD_COUNTER,
    ADD_TO_CART
} from './mutations-types'
export default {
    addCart(context, payload) {
        //添加新商品
        // let oldProduct = null
        // for (let item of state.cartList) {
        //     if (item.iid === payload.iid) {
        //         oldProduct = item
        //     }
        // }
        //面试题：数组常用的方法：push/pop/shift/unshift/sort/reverse/map/join/reduce/splice/indexOf/some
        return new Promise((resolve, reject) => {
            let oldProduct = context.state.cartList.find(function(item) {
                    return item.iid === payload.iid
                })
                //判断oldproduct
            if (oldProduct) {
                context.commit(ADD_COUNTER, oldProduct)
                resolve('当前的商品数量+1')
            } else {
                payload.count = 1;
                // state.cartList.push(payload)
                context.commit(ADD_TO_CART, payload)
                resolve('添加新的商品')

            }
        })
    }
}